package tuhu;

import java.util.Arrays;

/**
 * @author xizizzz
 * @description: TODO
 * @date 2021-8-31下午 07:40
 */
public class Test2 {

    public int closestValue(int[] array, int target) {
        int res = -1;
        int cha = Integer.MAX_VALUE;
        Arrays.sort(array);
        if (array.length == 0 || array.length == 1) {
            return -1;
        }
        if (array[0] + array[1] > target) {
            return -1;
        }
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {

                int tmp = Math.abs((array[i] + array[j]) - target);
                if (tmp == 0) {
                    return target;
                }
                if (tmp < cha) {
                    res = (array[i] + array[j]);
                    cha = tmp;
                }
            }
        }
        return res;
    }
}
